package com.kunya.product.service.impl.category;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.category.ProductCategoryMapper;
import com.kunya.product.po.ProductCategory;
import com.kunya.product.service.category.IProductCategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zp
 * @Description: 商品分类service实现类
 * @date 2018/6/13 18:18
 */
@Service("IProductCategoryService")
public class ProductCategoryServiceImpl implements IProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * @author zp
     * @Description: 根据商品id 查询商品规格表list
     * @param
     * @date 2018/6/13 18:42
     */
    public ServerResponse<List<ProductCategory>> getProductCatesByProductId(Long productId ,String tablePrefix){
        if(productId==null|| StringUtils.isEmpty(tablePrefix)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        List<ProductCategory> productCategoryList=productCategoryMapper.getProductCategoryByProductId(productId,tablePrefix);
        if (CollectionUtils.isEmpty(productCategoryList)) {
            return ServerResponse.createByErrorMessage("商品规格不存在");
        }
        return ServerResponse.createBySuccess(productCategoryList);
    }

    /**
     * @author zp
     * @Description: 根据分类id list 从os_product_category表获取商品id list
     * @param
     * @date 2018/6/13 18:55
     */
    public ServerResponse<List<Long>> getProductIdListByCategoryIds(List<Long> categoryIdList,String tablePrefix){
        if(CollectionUtils.isEmpty(categoryIdList)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), "参数错误");
        }
        List<Long> productIdList=productCategoryMapper.getProductIdListByCategoryIdList(categoryIdList,tablePrefix);
        if (CollectionUtils.isEmpty(productIdList)){
            return ServerResponse.createByErrorMessage("商品id列表不存在");
        }
        return ServerResponse.createBySuccess(productIdList);
    }

     /**
      * @author zp
      * @Description:批量插入商品规格
      * @param
      * @date 2018/6/17 18:31
      */
     @Transactional
     public ServerResponse<Integer>  saveProductCategorys(List<ProductCategory> productCategoryList,String tablePrefix){
         if(CollectionUtils.isEmpty(productCategoryList)){
             return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
         }
        int resultCount=productCategoryMapper.insertList(productCategoryList,tablePrefix);
        if(resultCount<=0){
            throw  new RuntimeException("添加商品规格失败");
        }
        return ServerResponse.createBySuccess(Integer.valueOf(resultCount));
     }

    @Override
    @Transactional
    public ServerResponse updateProductCategorys(Long productId,List<Long> categoryIds,String tablePrefix) {
         List<ProductCategory> productCategoryList=new ArrayList<>(categoryIds.size());
         for(int i=0;i<categoryIds.size();i++){
             ProductCategory productCategory=new ProductCategory();
             productCategory.setProductId(productId);
             //TODO 从Shrio当中获取当前用户名
             productCategory.setCreateBy("Suimg");
             productCategory.setCategoryId(categoryIds.get(i));
             productCategoryList.add(productCategory);
         }
         try {
             //删除原有的商品分类
             deleteProductCategorysByProductId(productId,tablePrefix);
             //重新添加新的分类
             productCategoryMapper.insertList(productCategoryList,tablePrefix);
             return ServerResponse.createBySuccess("修改商品分类成功");
         }catch (RuntimeException e){
             return ServerResponse.createByErrorMessage("修改商品分类失败");
         }
    }

    @Override
    public ServerResponse deleteProductCategorysByProductId(Long productId, String tablePrefix) {
        //删除原有的商品分类
        productCategoryMapper.deleteByProductId(productId,tablePrefix);
        return ServerResponse.createBySuccess("删除商品分类成功");
    }
}
